//# sourceURL=userList.js
layui.element.on('tab(login-user-tabs)', function () {
    var currPage = 1;
    var userList_fn = {
        initRender: function () {
            layui.table.render({
                id: 'userListTableId',
                elem: '#userList-table',
                url: '/cms/sysManage/getUserList',
                height: 'full-170',
                page: true,
                limits: cmsUtils.getLimits(),
                limit: cmsUtils.getLimit(),
                cols:[[
                    {title: '<b>序号</b>',type: 'numbers',align: 'center',width: 80},
                    {title: '<b>帐号</b>',field: 'username'},
                    {title: '<b>姓名</b>',field: 'name'},
                    {title: '<b>手机号码</b>',field: 'phone'},
                    {title: '<b>关联角色</b>',field: 'roleNames'},
                    {title: '<b>是否禁用</b>',field: 'locked',align: 'center',width: 90,templet: '#userList-locked-col-tpl'},
                    {title: '<b>创建时间</b>',field: 'dateCreated',align: 'center',width: 160},
                    {title: '<b>最近登录时间</b>',field: 'lastLogin',align: 'center',width: 160},
                    {title: '<b>操作选项</b>',align: 'center',toolbar: '#userList-toolbar'}
                ]],
                done: function (res, curr, count) {
                    currPage = curr;
                    layui.form.render('select');
                    $('#userList').attr('hasRender', true);
                }
            });
        },
        reload: function (page) {
            layui.table.reload('userListTableId', {
                page:{
                    curr: page ? page : currPage
                },
                where:{
                    roleId: $('#userList-select-role').val(),
                    locked: $('#userList-select-locked').val()
                }
            });
        },
        refreshList: function () {
            if ($('#userList')[0]) {
                userList_fn.reload(currPage);
            }
        },
        resetSearch: function () {
            cmsUtils.renderDynamicSelectionBox('/cms/sysManage/getRoles?addEmpty='+1, 'userList-select-role', 'id', 'name');
            $('#userList-select-locked').val('');
            layui.form.render('select');
            userList_fn.reload(1);
        }
    }
    if (!$('#userList').attr('hasRender')) {
        userList_fn.initRender();
        // 监听当前Tab页点击
        $(this).bind('tabClick', function () {
            userList_fn.refreshList();
        });
        // 搜索
        $('#userList-search-btn').click(function () {
            userList_fn.reload(1);
        });
        // 重置
        $('#userList-refresh-btn').click(function () {
            userList_fn.resetSearch();
        });
        // 新增
        $('#userList-add-btn').click(function () {
            cmsUtils.addContentTab('userDetail', '帐号-添加', '/piger/system/userDetail.html', {
                list_fn: userList_fn
            });
        });
    }
    cmsUtils.renderDynamicSelectionBox('/cms/sysManage/getRoles?addEmpty='+1, 'userList-select-role', 'id', 'name');
    // 工具条监听
    layui.table.on('tool(userList-table)', function (obj) {
        switch (obj.event) {
            case 'reset':
                layer.confirm('确定需要对该帐号进行密码重置？', {
                    btn:['确定', '取消']
                }, function (index) {
                    var post_url = '/cms/sysManage/user/'+obj.data.id+'/reset';
                    $.ajax({
                        type: 'post',
                        url: post_url,
                        dataType: 'json',
                        success: function (data) {
                            layer.close(index);
                            if (data.code != 200) {
                                layer.msg(data.msg, {anim: 6});
                            }
                        }
                    });
                });
                break;
            case 'edit':
                cmsUtils.addContentTab('userDetail', '帐号-编辑', '/piger/system/userDetail.html', {
                    id: obj.data.id,
                    list_fn: userList_fn
                });
                break;
            case 'enable':
                layer.confirm('确定启用该帐号？启用后该帐号可再次登录！', {
                    btn:['确定', '取消']
                }, function (index) {
                    var post_url = '/cms/sysManage/user/'+obj.data.id+'/unlock';
                    $.ajax({
                        type: 'post',
                        url: post_url,
                        dataType: 'json',
                        success: function (data) {
                            layer.close(index);
                            if (data.code == 200) {
                                userList_fn.reload(currPage);
                            } else {
                                layer.msg(data.msg, {anim: 6});
                            }
                        }
                    });
                });
                break;
            case 'disable':
                layer.confirm('确定禁用该帐号？请确认该帐号下已无待处理业务，禁用后该帐号将无法再登录！', {
                    btn:['确定', '取消']
                }, function (index) {
                    var post_url = '/cms/sysManage/user/'+obj.data.id+'/lock';
                    $.ajax({
                        type: 'post',
                        url: post_url,
                        dataType: 'json',
                        success: function (data) {
                            layer.close(index);
                            if (data.code == 200) {
                                userList_fn.reload(currPage);
                            } else {
                                layer.msg(data.msg, {anim: 6});
                            }
                        }
                    });
                });
                break;
            case 'delete':
                layer.confirm('确定删除该帐号？', {
                    btn:['确定', '取消']
                }, function (index) {
                    var post_url = '/cms/sysManage/user/'+obj.data.id+'/delete';
                    $.ajax({
                        type: 'post',
                        url: post_url,
                        dataType: 'json',
                        success: function (data) {
                            layer.close(index);
                            if (data.code == 200) {
                                userList_fn.reload(currPage);
                            } else {
                                layer.msg(data.msg, {anim: 6});
                            }
                        }
                    });
                });
                break;
            default:
                break;
        }
    });
});